package com.gxuwz.Market.business.dao;

import org.springframework.stereotype.Repository;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;

import org.hibernate.LockMode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.gxuwz.Market.business.entity.ServiceForm;
import com.gxuwz.Market.business.entity.TemplateUpload;
import com.gxuwz.core.dao.impl.BaseDaoImpl;
import com.gxuwz.core.pagination.Result;



@Repository("serviceFormDAO")
public class ServiceFormDAO extends BaseDaoImpl<ServiceForm>{
	/**
	 * 根据条件查找分页
	 * @param serviceForm 模型
	 * @param page 第几页
	 * @param row 长度
	 * @return
	 * @author 李宪乾
	 * @date 2017.9.27
	 */
	@SuppressWarnings("unchecked")
	public Result<ServiceForm> find(ServiceForm serviceForm, int page, int size){
		String queryString = "from ServiceForm where isDelete=0";
		if(null != serviceForm.getId() && !"".equals(serviceForm.getId())){			
			queryString = queryString + "and Id like '%"+serviceForm.getId()+"%'";
		}
		if(null != serviceForm.getFormId() && !"".equals(serviceForm.getFormId())){
			queryString = queryString +" and fromId like '%"+serviceForm.getFormId()+"%'";
		}
		if(null != serviceForm.getFieldNames() && !"".equals(serviceForm.getFieldNames())){
			queryString = queryString +" and fieldNames like '%"+serviceForm.getFieldNames()+"%'";
		}
		if(null != serviceForm.getFieldNum() && !"".equals(serviceForm.getFieldNum())){
			queryString = queryString +" and fieldNum like '%"+serviceForm.getFieldNum()+"%'";
		}
		if(null != serviceForm.getFieldType() && !"".equals(serviceForm.getFieldType())){
			queryString = queryString +" and fieldType like '%"+serviceForm.getFieldType()+"%'";
		}
		if(null != serviceForm.getFormName() && !"".equals(serviceForm.getFormName())){
			queryString = queryString +" and formName like '%"+serviceForm.getFormName()+"%'";
		}
		if(null != serviceForm.getCreaterId() && !"".equals(serviceForm.getCreaterId())){
			queryString = queryString +" and createrId like '%"+serviceForm.getCreaterId()+"%'";
		}
		if(null != serviceForm.getCreateTime() && !"".equals(serviceForm.getCreateTime())){
			queryString = queryString +" and createTime like '%"+serviceForm.getCreateTime()+"%'";
		}
		if(null != serviceForm.getFormState() && !"".equals(serviceForm.getFormState())){
			queryString = queryString +" and formState like '%"+serviceForm.getFormState()+"%'";
		}
		if(null != serviceForm.getFieldLen() && !"".equals(serviceForm.getFieldLen())){
			queryString = queryString +" and fieldLen like '%"+serviceForm.getFieldLen()+"%'";
		}
		if(null != serviceForm.getRemark() && !"".equals(serviceForm.getRemark())){
			queryString = queryString +" and remark like '%"+serviceForm.getRemark()+"%'";
		}
		int start=(page-1)*size;
		int limit =size;
		return (Result<ServiceForm> )super.find(queryString, null, null, start, limit);
	}
	
	public List<ServiceForm> findAll() {
		List<ServiceForm> serviceClassList = null;
		String queryString = "from ServiceForm where isDelete=0";
		serviceClassList = this.getHibernateTemplate().find(queryString);
		return serviceClassList;
	}
	/**
	 * 根据id查询单个表格
	 * @param userId
	 * @return
	 */
	public ServiceForm findById(int id){
		List<ServiceForm> serviceClassList = null;
		String queryString = "from ServiceForm where id ="+id;
		try{
			serviceClassList = this.getHibernateTemplate().find(queryString);
		}catch(Exception e){
			System.out.println(e);
		}
		return serviceClassList.get(0);
	}
	
	public List<ServiceForm> findByProperty(String property, String value){
		List<ServiceForm> list = null;
		String queryString = "from ServiceForm where "+property+" ='"+value+"' ";
		try{
			list = this.getHibernateTemplate().find(queryString);
		}catch(Exception e){
			System.out.println(e);
		}
		return list;
	}

	/**
	 * 通过文件的Id查找文件
	 * @return
	 * @author 黄森南
	 * @throws FileNotFoundException 
	 * @throws IOException 
	 * @date 2018.01.27
	 */
	public TemplateUpload findByFid(Integer fileId) {
	String hql =" from  TemplateUpload where id='"+fileId+"' ";
		return (TemplateUpload) getHibernateTemplate().find(hql).get(0);
	}
}